package com.frxuexi.mdm2204.myview

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.animation.CycleInterpolator
import android.widget.ImageView
import android.widget.RelativeLayout
import com.frxuexi.mdm2204.R

class MyZanView:RelativeLayout {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        val x = event?.x
        val y = event?.y
        if(event?.action==MotionEvent.ACTION_DOWN){
            //手按下时，开启点赞动画
            start(x!!,y!!)
        }
        return true
    }

    fun start(x:Float,y:Float){
        val iv = ImageView(context)
        iv.setImageResource(R.drawable.ic_action_zan)

        val params = LayoutParams(200, 200)
        params.leftMargin=Math.round(x)-100
        params.topMargin=Math.round(y)-200
        iv.layoutParams=params

        val animator = ObjectAnimator.ofFloat(iv, "alpha", 1f, 0f)
        val animator1 = ObjectAnimator.ofFloat(iv, "rotation", x, 100f, x)
        animator1.interpolator=CycleInterpolator(5f)
        val animator2 = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 2f)
        val animator3 = ObjectAnimator.ofFloat(iv, "scaleY", 1f, 2f)

        val set = AnimatorSet()
        set.play(animator)
            .with(animator1)
            .with(animator2)
            .with(animator3)
        set.duration=3000
        set.start()
        addView(iv)

    }
}